using System;
using System.Collections.Generic;
using Wikey.Extensibility.Interfaces;

namespace Wikey.Transformation
{
    public class DictionaryTranformerRepository : ITransformerRepository {
        
        private Dictionary<string, Type> TransformerCollectionType;

        public DictionaryTranformerRepository() {
            RegisterTransformersDynamically();
        }

        public void RegisterTransformersDynamically()
        {
            if( TransformerCollectionType == null )
            {
                TransformerCollectionType = TransformationPluginManager.Instance.TransformerCollection;
            }
        }
        

        public ITransformer Find(IExpressionFunction function) {
            return FindDynamically(function);
        }

        public ITransformer FindDynamically( IExpressionFunction function )
        {
            if (TransformerCollectionType.ContainsKey(function.Name))
            {
                return Activator.CreateInstance(TransformerCollectionType[function.Name]) as ITransformer;
            }

            return null;
            throw new TransformerNotFoundException(
                String.Format(" Transformer for Command:{0} not found!", function.Name)
                );
        }
    }

}